# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.point_logDao import PointLogDao
import time
from . import app
from com.zzh.dao_v2.AuthTokenOperation import authTokenOperation

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)



@app.route('/addPointLog', methods=['POST'])
def addPointLogNoToken():
    if not request.json:
        abort(400)

    point_log_info = {
        "user_id": request.json['user_id'],
        "username": request.json['username'],
        "point_item_name": request.json['point_item_name'],
        "point_value": request.json['point_value'],
        "remark": request.json['remark'],
        "is_income": request.json['is_income'],
        "create_time": request.json['create_time']
    }
    try:
        rs = PointLogDao().add_point_log(point_log_info)

        if rs:
            return jsonify({  "message": {"code": 1, "info": "添加成功！"}})
        else:
            return jsonify({"message": {"code": 0, "info": "添加失败！"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/delPointLog', methods=['POST'])
def delPointLogNoToken():

    """

     if not request.json:
        abort(400)
    ids = request.json.get['ids']
    try:
        rs=PointLogDao().del_point_log(ids)
    except:
        return
    return jsonify({"message": {"code": 1, "info": " delete OK"}})
"""
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message": "请输入待删除的  ids  数组！"})
    try:
        rs = PointLogDao().del_point_log(ids)
        if rs:
            return jsonify({"message": {"code": 1, "info": "delete  OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "delete  Error"}})
    except Exception, e:
        return jsonify({"message": {"code": 0, "info": str(e)}})


@app.route('/queryPointLogById', methods=['POST'])
def queryPointLogByIdNoToken():

    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        total_count, userCodeJSON= PointLogDao().query_point_log(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryShare OK"},"info":userCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})



@app.route('/queryPointLogByCondition', methods=['POST'])
def queryPointLogByConditionNoToken():
        if not request.json:
            abort(400)


        try:
            page = request.json['page']
            pageIndex = page["pageIndex"]
            pageSize = page["pageSize"]
        except:
            return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
        try:
            conditions = request.json['condition']
        except:
            return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
        try:
            multiSort = request.json['multiSort']

        except:
            return jsonify({"message": "未传入排序规则multiSort"})
        userId = request.json.get("userId", 1)
        try:
            total_count, UrlManageJSON = PointLogDao().queryPointLogByCondition(pageIndex=pageIndex, pageSize=pageSize,
                                                                                conditions=conditions, multiSort=multiSort,
                                                                                userId=userId)
            if total_count:
                return jsonify(
                    {"message": {"code": 1, "status": "queryPointLog OK"}, "total": total_count, "info": UrlManageJSON})
            else:
                return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
        except Exception, e:
            return jsonify({"message": {"code": 0, "info": str(e)}})




@app.route('/updatePointLog', methods=['POST'])
def updatePointLogNoToken():
    try:
        id =    request.json['id']
    except:
        return jsonify({"messgae":"请传入 id"})

    try:
        oldInfo = PointLogDao().selectPointLog(id)
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

    new_point_log = {
        "user_id": request.json.get('user_id',oldInfo[1]),
        "username": request.json.get('username',oldInfo[2]),
        "point_item_name": request.json.get('point_item_name',oldInfo[3]),
        "point_value": request.json.get('point_value',oldInfo[4]),
        "remark": request.json.get('remark',oldInfo[5]),
        "is_income": request.json.get('is_income',oldInfo[6]),
        "create_time": request.json.get('create_time',oldInfo[7])
    }
    if not id:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        updateresult = PointLogDao().update_point_log(new_point_log, id)
        if updateresult:
            return jsonify({"info": updateresult,
                            "message": {"code": 1, "info": "update OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "update Failed"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})
